<?php
if ( IPB_THIS_SCRIPT != 'admin' )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'init.php'.";
	exit();
}

class ad_group
{
	var $ipsclass;
	
	function auto_run()
	{
		$redirect = array(
									'list' => 'list_group',
									'edit' => 'edit_group',
									'save' => 'save_group',
										);
		if(!$redirect[$this->ipsclass->input['do']])
		{
			$this->ipsclass->input['do'] = 'list';
		}
		$run = $redirect[$this->ipsclass->input['do']];
		$this->$run();
	}
	
	function list_group()
	{
		$this->ipsclass->input['group_id'] = $this->ipsclass->input['group_id'] > 0 ? intval($this->ipsclass->input['group_id']) : $this->ipsclass->vars['member_group'];
		
		$this->ipsclass->admin->page_title  = "Group Listing";
		$this->ipsclass->admin->page_detail = "Here you can choose a group to edit for Army System 2.2 and edit the group";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code.'&amp;code=groups', 'Manage Groups' );
		
		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "60%" );
		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "40%" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Group List");
		
		$groups = array();
		foreach($this->ipsclass->cache['group_cache'] as $gid => $group)
		{
			$groups[] = array($gid, $group['g_title']);
		}

		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>What group do you want to edit?</b>",
																			  $this->ipsclass->adskin->form_dropdown("group_id",$groups,$this->ipsclass->input['group_id'],"onchange='change_group()' id='group_id'")
																	 )      );

		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->html .= "<div id='edit_group_fields'></div>\n";

		$this->ipsclass->html .= "<script type=\"text/javascript\">\n";
		$this->ipsclass->html .= "	change_group();\n";
		$this->ipsclass->html .= "</script>\n";
		
		$this->ipsclass->admin->output();
	}
	
	function edit_group()
	{
		$html = "";
		
		if(!$this->ipsclass->cache['group_cache'][$this->ipsclass->input['group_id']])
		{
			$print = $this->ipsclass->adskin->start_table("Error");
			$print .= $this->ipsclass->adskin->add_td_row( array("Cannot the group you are looking for.") );
			$print .= $this->ipsclass->adskin->end_table();
			echo $print;
			exit();
		}
		
		$html .= $this->ipsclass->adskin->start_form( array( 1 => array( 'section'   , 'components'  ),
				 	 	 	 	 	 	 	 	 	  	  	 2 => array( 'act'       , 'market'      ),
															 3 => array( 'code'      , 'groups'      ),
															 5 => array( 'do'        , 'save'        ),
															 6 => array( 'group_id'  , $this->ipsclass->input['group_id'] ),
													 )      );
		
		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "40%" );
		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "60%" );
		
		$html .= $this->ipsclass->adskin->start_table("Group Details");
		
		$html .= $this->ipsclass->adskin->add_td_row( array( "<b>Group Discount</b><br />This will give the group a discount on all bought stuff." ,
													  $this->ipsclass->adskin->form_simple_input( "g_market_discount",$this->ipsclass->cache['group_cache'][$this->ipsclass->input['group_id']]['g_market_discount'], 4 ).'%'
											 )      );

		$html .= $this->ipsclass->adskin->add_td_row( array( "<b>Group Tax</b><br />This will add on to any tax on any item in the shop area." ,
													  $this->ipsclass->adskin->form_simple_input( "g_market_tax",$this->ipsclass->cache['group_cache'][$this->ipsclass->input['group_id']]['g_market_tax'] ).'%'
											 )      );

		$html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->adskin->td_header[] = array( "ibMarket/Plugin"  , "34%" );
		$this->ipsclass->adskin->td_header[] = array( "View Plugin"      , "33%" );
		$this->ipsclass->adskin->td_header[] = array( "Use Plugin"       , "33%" );
		
		$html .= $this->ipsclass->adskin->start_table("Permissions");
		
		if($this->ipsclass->input['group_id'] == $this->ipsclass->vars['admin_group'])
		{
			$html .= $this->ipsclass->adskin->add_td_row(array(
																"<div align='right'>".$this->ipsclass->vars['market_name']."</div>",
																"<div align='center' style='border:1px solid #777777;background-color: #DFE6EF'>".$this->ipsclass->adskin->form_checkbox('VIEW[ibmarket]',1,1,array("style='border: none'","disabled='disabled'"))."</div>",
																"<div align='center' style='border:1px solid #777777;background-color: #f5cdcd'>".$this->ipsclass->adskin->form_checkbox('USE[ibmarket]',1,1,array("style='border: none'","disabled='disabled'"))."</div>",
														)	);
		}
		elseif($this->ipsclass->input['group_id'] == $this->ipsclass->vars['guest_group'])
		{
			$html .= $this->ipsclass->adskin->add_td_row(array(
																"<div align='right'>".$this->ipsclass->vars['market_name']."</div>",
																"<div align='center' style='border:1px solid #777777;background-color: #DFE6EF'>".$this->ipsclass->adskin->form_checkbox('VIEW[ibmarket]',0,1,array("style='border: none'","disabled='disabled'"))."</div>",
																"<div align='center' style='border:1px solid #777777;background-color: #f5cdcd'>".$this->ipsclass->adskin->form_checkbox('USE[ibmarket]',0,1,array("style='border: none'","disabled='disabled'"))."</div>",
														)	);
		}
		else
		{
			$html .= $this->ipsclass->adskin->add_td_row(array(
																"<div align='right'>".$this->ipsclass->vars['market_name']."</div>",
																"<div align='center' style='border:1px solid #777777;background-color: #DFE6EF'>".$this->ipsclass->adskin->form_checkbox('VIEW[ibmarket]',$this->ipsclass->cache['group_cache'][$this->ipsclass->input['group_id']]['g_market_view'],1,array("style='border: none'"))."</div>",
																"<div align='center' style='border:1px solid #777777;background-color: #f5cdcd'>".$this->ipsclass->adskin->form_checkbox('USE[ibmarket]',$this->ipsclass->cache['group_cache'][$this->ipsclass->input['group_id']]['g_market_use'],1,array("style='border: none'"))."</div>",
														)	);
		}
		
		foreach($this->ipsclass->cache['market_plugin'] as $plugin)
		{
			if(in_array(-1,$plugin['view_permission']))
			{
				$VIEW = "<div align='center' style='border:1px solid #777777;background-color: #DFE6EF'><i>GLOBAL</i></div>";
			}
			else
			{
				$VIEW = "<div align='center' style='border:1px solid #777777;background-color: #DFE6EF'>".$this->ipsclass->adskin->form_checkbox('VIEW['.$plugin['link'].']',in_array($this->ipsclass->input['group_id'],$plugin['view_permission']),1,array("style='border: none'"))."</div>";
			}
			if(in_array(-1,$plugin['use_permission']))
			{
				$USE = "<div align='center' style='border:1px solid #777777;background-color: #f5cdcd'><i>GLOBAL</i></div>";
			}
			else
			{
				$USE = "<div align='center' style='border:1px solid #777777;background-color: #f5cdcd'>".$this->ipsclass->adskin->form_checkbox('USE['.$plugin['link'].']',in_array($this->ipsclass->input['group_id'],$plugin['use_permission']),1,array("style='border: none'"))."</div>";
			}
			
			$html .= $this->ipsclass->adskin->add_td_row(array(
																"<div align='right'>".$plugin['name']."</div>",
																$VIEW,
																$USE,
														)	);
		}
		
		$html .= $this->ipsclass->adskin->end_table();
		
		$html .= $this->ipsclass->adskin->end_form_standalone("Save group details");
		
		print $html;
		exit();
	}
	
	function save_group()
	{
		if(!$this->ipsclass->cache['group_cache'][$this->ipsclass->input['group_id']])
		{
			$this->ipsclass->admin->error("Cannot the group you are looking for.");
		}
		
		if($this->ipsclass->input['group_id'] == $this->ipsclass->vars['admin_group'])
		{
			$this->ipsclass->input['VIEW']['ibmarket'] = 1;
			$this->ipsclass->input['USE']['ibmarket'] = 1;
		}
		elseif($this->ipsclass->input['group_id'] == $this->ipsclass->vars['guest_group'])
		{
			$this->ipsclass->input['VIEW']['ibmarket'] = 0;
			$this->ipsclass->input['USE']['ibmarket'] = 0;
		}
		
		$save = array(
						'g_market_view'       => intval($this->ipsclass->input['VIEW']['ibmarket'])                                  ,
						'g_market_use'        => intval($this->ipsclass->input['USE']['ibmarket'])                                   ,
						'g_market_discount'   => $this->ipsclass->input['g_market_discount']                                         ,
						'g_market_tax'        => $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['g_market_tax']),
					);
		
		$this->ipsclass->DB->do_update('groups',$save,"g_id=".$this->ipsclass->input['group_id']);
		
		$this->ipsclass->market_class->group_cache();
		
		foreach($this->ipsclass->cache['market_plugin'] as $plugin)
		{
			$save_plugin = array();
			if(!in_array(-1,$plugin['view_permission']))
			{
				if($this->ipsclass->input['VIEW'][$plugin['link']] && !in_array($this->ipsclass->input['group_id'],$plugin['view_permission']))
				{
					$save_plugin['view_permission'] = array();
					foreach($this->ipsclass->cache['group_cache'] as $group)
					{
						if(in_array($group['g_id'],$plugin['view_permission']) || $group['g_id'] == $this->ipsclass->input['group_id'])
						{
							$save_plugin['view_permission'][] = $group['g_id'];
						}
					}
					$save_plugin['view_permission'] = implode(",",$save_plugin['view_permission']);
				}
				elseif(!$this->ipsclass->input['VIEW'][$plugin['link']] && in_array($this->ipsclass->input['group_id'],$plugin['view_permission']))
				{
					foreach($this->ipsclass->cache['group_cache'] as $group)
					{
						if(in_array($group['g_id'],$plugin['view_permission']) || $group['g_id'] != $this->ipsclass->input['group_id'])
						{
							$save_plugin['view_permission'][] = $group['g_id'];
						}
					}
					$save_plugin['view_permission'] = implode(",",$save_plugin['view_permission']);
				}
			}
			if(!in_array(-1,$plugin['use_permission']))
			{
				if($this->ipsclass->input['USE'][$plugin['link']] && !in_array($this->ipsclass->input['group_id'],$plugin['use_permission']))
				{
					$save_plugin['use_permission'] = array();
					foreach($this->ipsclass->cache['group_cache'] as $group)
					{
						if(in_array($group['g_id'],$plugin['use_permission']) || $group['g_id'] == $this->ipsclass->input['group_id'])
						{
							$save_plugin['use_permission'][] = $group['g_id'];
						}
					}
					$save_plugin['use_permission'] = implode(",",$save_plugin['use_permission']);
				}
				elseif(!$this->ipsclass->input['USE'][$plugin['link']] && in_array($this->ipsclass->input['group_id'],$plugin['use_permission']))
				{
					foreach($this->ipsclass->cache['group_cache'] as $group)
					{
						if(in_array($group['g_id'],$plugin['use_permission']) || $group['g_id'] != $this->ipsclass->input['group_id'])
						{
							$save_plugin['use_permission'][] = $group['g_id'];
						}
					}
					$save_plugin['use_permission'] = implode(",",$save_plugin['use_permission']);
				}
			}
			if(count($save_plugin))
			{
				$this->ipsclass->DB->do_update('market_plugins',$save_plugin,"link='".$plugin['link']."'");
			}
		}
		
		$this->ipsclass->market_class->cache_plugins();
		
		$this->ipsclass->admin->save_log("ibMarket: Editted group ".$this->ipsclass->cache['group_cache'][$this->ipsclass->input['group_id']]['g_title']);
		
		$this->ipsclass->main_msg = "Editing <b>".$this->ipsclass->cache['group_cache'][$this->ipsclass->input['group_id']]['prefix'].$this->ipsclass->cache['group_cache'][$this->ipsclass->input['group_id']]['g_title'].$this->ipsclass->cache['group_cache'][$this->ipsclass->input['group_id']]['suffix']."</b> done.";
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=groups&do=list&group_id=".$this->ipsclass->input['group_id'],"Done editing the group, loading edit page");
	}
}
?>